//
//  YYUMeng.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/8/15.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import Foundation

final class YYUMeng {
    
    static func shareAction(title: String, des: String, thumbImage: UIImage, url: String) {
        UMSocialUIManager.showShareMenuViewInWindow { (platformType, userInfo) -> Void in
            let shareObject: UMShareWebpageObject = UMShareWebpageObject()
            shareObject.title = title
            shareObject.descr = des
            shareObject.thumbImage = thumbImage
            shareObject.webpageUrl = url
            
            let messageObject:UMSocialMessageObject = UMSocialMessageObject()
            messageObject.text = "社会化组件UShare将各大社交平台接入您的应用，快速武装App。"
            messageObject.shareObject = shareObject;
            
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: nil, completion: { (shareResponse, error) -> Void in
                if error != nil {
                    SVProgressHUD.showError(with: "分享失败")
                } else {
                    SVProgressHUD.showSuccess(with: "分享成功")
                }
            })
        }
    }
    
    static func wxLogin(completionHandler: @escaping (Bool, (type: String, uid: String, openid: String, name: String, iconurl: String)) -> Void)  {
        UMSocialManager.default().getUserInfo(with: .wechatSession, currentViewController: nil) { (info, error) in
            if let resp = info as? UMSocialUserInfoResponse {
                completionHandler(true, (type: "wx", uid: resp.uid!, openid: resp.openid!, name: resp.name!, iconurl: resp.iconurl!))
            } else {
                completionHandler(false, (type: "", uid: "", openid: "", name: "", iconurl: ""))
            }
        }
    }
    
    static func qqLogin(completionHandler: @escaping (Bool, (type: String, uid: String, openid: String, name: String, iconurl: String)) -> Void)  {
        UMSocialManager.default().getUserInfo(with: .QQ, currentViewController: nil) { (info, error) in
            if let resp = info as? UMSocialUserInfoResponse {
                completionHandler(true, (type: "qq", uid: resp.unionId ?? "", openid: resp.openid!, name: resp.name!, iconurl: resp.iconurl!))
            } else {
                completionHandler(false, (type: "", uid: "", openid: "", name: "", iconurl: ""))
            }
        }
    }
    
}
